package productrouter

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"nodeseller/RabbitMQ"
	myredis "nodeseller/redis"
	"strconv"
	"time"

	"nodeseller/datamodels"
	"nodeseller/repositories"
	"nodeseller/response"
)

//创建订单
func CreateOrder(c *gin.Context)  {
	var orderdata datamodels.Product
	err :=  c.ShouldBind(&orderdata)
	if err != nil{
		response.Response1(c,1,"创建失败，请稍后再试","")
		return
	}
	if orderdata.Price <= 0 || orderdata.Number <= 0{
		response.Response1(c,1,"创建失败，请稍后再试","")
		return
	}
	phone ,err := c.Cookie("phone")
	if err != nil{
		response.Response1(c,1,"身份验证失败，请重新登陆","")
		return
	}
	file,err := c.FormFile("file")
	if err != nil{
		response.Response1(c,1,"参数错误，请稍后重试1","")
		return
	}
	err = c.SaveUploadedFile(file,"./public/images/"+file.Filename)
	if err != nil{
		response.Response1(c,1,"参数错误，请稍后重试2","")
		return
	}
	orderdata.MainImg = "/static/images/"+strconv.FormatInt(time.Now().Unix(),10)+file.Filename
	orderdata.PhoneNumber = phone
	if err != nil{
		response.Response1(c,1,"身份验证失败，请重新登陆","")
		return
	}
	user := repositories.NewUser()
	result,err := user.SelectByKey(phone)
	if err != nil{
		response.Response1(c,1,"创建失败，请稍后再试3","")
		return
	}
	if result.Cash < orderdata.Price * float32(orderdata.Number){
		response.Response1(c,1,"积分不足，请先充值","")
		return
	}
	err = user.Update(phone,orderdata.Price * float32(orderdata.Number))
	if err != nil{
		fmt.Println(err)
		response.Response1(c,1,"创建失败，请稍后再试4","")
		return
	}
	var billData datamodels.Bill
	billData.Cash = -orderdata.Price * float32(orderdata.Number)
	billData.PhoneNumber = phone
	billData.Types = "发单扣款"
	billData.Time = time.Now().Format("2006-01-02 15:04:05")
	rabbitmq := RabbitMQ.NewRabbitMQSimple("createproduct")
	bill := RabbitMQ.NewRabbitMQSimple("bill")
	billresult,err := json.Marshal(billData)
	bill.PublishSimple(string(billresult))
	data,err := json.Marshal(orderdata)
	if err != nil{
		response.Response1(c,1,"创建失败，请稍后再试5","")
		return
	}
	rabbitmq.PublishSimple(string(data))
	response.Response1(c,0,"创建成功","")
}
//删除订单
func DeleteOrder(c *gin.Context)  {
	phone,err := c.Cookie("phone")
	if err != nil{
		response.Response1(c,1,"身份验证失败，请重新登陆","")
		return
	}
	productid := c.PostForm("_id")
	if productid == ""{
		response.Response1(c,1,"参数丢失，请稍后重试","")
		return
	}
	id,err := primitive.ObjectIDFromHex(productid)
	if err != nil{
		response.Response1(c,1,"参数丢失，请稍后重试","")
		return
	}
	product := repositories.NewProductManager()
	result := product.Delete(id)
	if result{
		rdb := myredis.Get()
		ret,err := rdb.ZRevRangeWithScores(productid,0,1).Result()
		if err != nil{
			response.Response1(c,1,"删除订单失败","")
			return
		}
		var sum float32
		sum = float32(ret[0].Score) * float32(ret[1].Score)
		rdb.Del(productid)
		rabbitmq := RabbitMQ.NewRabbitMQSimple("deleteproduct")
		rabbitmq.PublishSimple(`{"cash":`+fmt.Sprintf("%v",sum)+`,"phone":"`+phone+`"}`)
		var billData datamodels.Bill
		billData.Cash = sum
		billData.PhoneNumber = phone
		billData.Types = "下架退款"
		billData.Time = time.Now().Format("2006-01-02 15:04:05")
		bill := RabbitMQ.NewRabbitMQSimple("bill")
		billresult,err := json.Marshal(billData)
		bill.PublishSimple(string(billresult))
		response.Response1(c,0,"删除成功","")
		return
	}
	response.Response1(c,1,"删除失败","")
}
//查看正在进行中的产品
func FindAllDoing(c *gin.Context)  {
	phone,err := c.Cookie("phone")
	if err != nil{
		response.Response1(c,1,"身份验证失败，请重新登陆","")
		return
	}
	result,err := repositories.NewProductManager().SelectAll(phone)
	if err != nil{
		response.Response1(c,1,"数据错误，请稍后重试","")
		return
	}
	response.Response1(c,0,"查询成功",result)
}
//查询已经完成的订单
func FindAllFined(c *gin.Context)  {
	phone,err := c.Cookie("phone")
	if err != nil{
		response.Response1(c,1,"身份验证失败，请重新登陆","")
		return
	}
	result,err := repositories.NewProductManager().OrderFined(phone)
	if err != nil{
		response.Response1(c,1,"数据错误，请稍后重试","")
		return
	}
	response.Response1(c,0,"查询成功",result)
}
//查询已经删除的订单
func FindAllDelete(c *gin.Context)  {
	phone,err := c.Cookie("phone")
	if err != nil{
		response.Response1(c,1,"身份验证失败，请重新登陆","")
		return
	}
	result,err := repositories.NewProductManager().FindDelete(phone)
	if err != nil{
		response.Response1(c,1,"数据错误，请稍后重试","")
		return
	}
	response.Response1(c,0,"查询成功",result)
}
